package com.example.demo.lucene;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class LuceneConfig {

    private static LuceneConfig  CONFIG=null;
    public final static LuceneConfig getInstance(){
        if (CONFIG!=null)return CONFIG;
        LuceneConfig luceneConfig = new LuceneConfig();
        Configurations configurations = new Configurations();
        File file = new File("config.properties");
        try {
            PropertiesConfiguration properties = configurations.properties(file);
            Map<String,Object> param =new HashMap<>();
            Iterator<String> keys = properties.getKeys();
            while (keys.hasNext()){
                String next = keys.next();
                param.put(next,properties.getProperty(next));
            }
            BeanUtils.populate(luceneConfig,param);
        } catch (ConfigurationException | IllegalAccessException | InvocationTargetException e) {
            System.out.println("找不到匹配的文件："+file.getPath());
            return null;
        }
        return luceneConfig;
    }
    private LuceneConfig(){}
    private String path;//数据存储地址
    private int size;//文件大小
    private String table;//默认表名称

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }
}
